<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
$day = $_POST['day'];
$month = $_POST['month'];
$staffId = $_POST['staffId'];

$startHour = $_POST['startHour_'.$staffId];
$startMinute = $_POST['startMinute_'.$staffId];
$endHour = $_POST['endHour_'.$staffId];
$endMinute = $_POST['endMinute_'.$staffId];

$tmp_date=date("Ymd");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;

if( $month < $tmp_mon ){
	Logs::setLog(" month is invalide ");
	Output::Json("month is invalide", 1);
}
$data = null;
$result = 0;

$tempStaff = Staff::getById($staffId);
foreach($day as $k => $v){
	Logs::setLog(" loop start ");
	if( $v < $tmp_day ){
		Logs::setLog(" continue is perior day .$v || $tmp_day");
		$data = $data."欠勤追加失敗しました(｜｜$tmp_year, $month, $v)。  ";
		$result = 1;
		continue;
	}
	if( $startHour[$k] == 0 ){
		$tempA = Absence::GetByYMD($staffId, $tmp_year, $month, $v);
		Absence::DeleteById($tempA['id']);
	}else if( $startHour[$k] != 0 && $startHour[$k] != null){
		$workSchedule = WorkSchedule::GetByYMD($staffId, $tmp_year, $month, $v);//($staffId, $tmp_year, $month, $v, $startHour[$k], $startMinute[$k], $endHour[$k], $endMinute[$k]);
		if( $workSchedule == null){
			Logs::setLog(" workSchedule is null ");
			$data = $data."欠勤追加失敗しました(通勤記録はありません｜｜$tmp_year, $month, $v)。  ";
			$result = 1;
			continue;
		}
		$tempStartHour = date("H", $workSchedule['start_time']) * 1;
		$tempStartMinute = date("i", $workSchedule['start_time']) * 1;
		$tempEndHour = date("H", $workSchedule['end_time']) * 1;
		$tempEndMinute = date("i", $workSchedule['end_time']) * 1;
		if( $tempStartHour > $startHour[$k] || $tempEndHour < $endHour[$k]){
			Logs::setLog(" time invalide compare 2 workSchedule ");
			$data = $data."欠勤追加失敗しました(欠勤時間不正｜｜$tmp_year, $month, $v)。  ";
			$result = 1;
			continue;
		}
		if( ($tempStartHour == $startHour[$k] && $tempStartMinute > $startMinute[$k]) || 
		  ($tempEndHour == $endHour[$k] && $tempEndMinute > $endMinute[$k]) ){
		  	$data = $data."欠勤追加失敗しました(欠勤時間不正｜｜$tmp_year, $month, $v)。  ";
		  	Logs::setLog(" time invalide compare 2 workSchedule 37");
		  	$result = 1;
			continue;  
		}
		if( $startHour[$k] > $endHour[$k] || ($startHour[$k] == $endHour[$k] && $startMinute[$k] >= $endMinute[$k]) ){
			Logs::setLog(" time invalide from input ".$startHour[$k]."||".$endHour[$k]."||".$startMinute[$k]."||".$endMinute[$k]);
			$data = $data."欠勤追加失敗しました(欠勤時間不正｜｜$tmp_year, $month, $v)。  ";
			$result = 1;
			continue;
		}
		$startTime = mktime($startHour[$k], $startMinute[$k], 0, $month, $v, $tmp_year);
		$endTime = mktime($endHour[$k], $endMinute[$k], 0, $month, $v, $tmp_year);
		
		$temp = Preengage::HasOrdered($startTime, $endTime, $staffId);
		if( $temp != null ){
			$data = $data."欠勤追加失敗しました(予約があります｜｜$tmp_year, $month, $v)。  ";
			$result = 1;
			continue;
		}
		
		Logs::setLog(" insert absence workSchedule ");
		Absence::Create($staffId, $tmp_year, $month, $v, $startHour[$k], $startMinute[$k], $endHour[$k], $endMinute[$k]);
		$tempUser = Users::GetUser($login_user_id);
		
		$log = $tempUser['family_name']." ".$tempUser['personal_name']."は".$tempStaff['family_name']." ".$tempStaff['personal_name']."の$tmp_year-$month-".$v."の臨時休みを登録しました。";
		AccessHistory::Create($login_user_id, $log);
		
	}
}
if( $result == 0 ){
	Output::Json(0);
}else{
	Output::Json($data, 1);
}
